﻿using B.S.Shop.Application.Command;
using B.S.Shop.Domain;
using B.S.Shop.ErrorCode;
using B.S.Shop.Infartructrue.Interfaces;
using MediatR;

namespace B.S.Shop.Application.Commandhandler
{
    public class OrderUpdateCommandhandler : IRequestHandler<OrderUpdateCommand, APIResult<int>>
    {
        public OrderUpdateCommandhandler(IBaseRepository<OrderClass> OrderRepository)
        {
            this.OrderRepository = OrderRepository;
        }

        public IBaseRepository<OrderClass> OrderRepository { get; }

        public async Task<APIResult<int>> Handle(OrderUpdateCommand request, CancellationToken cancellationToken)
        {
            var str = request.Ids.Split(',').Select(a => Convert.ToInt32(a)).ToList();
            var path = OrderRepository.GetAll().Where(a => str.Contains(a.OrderId)).ToList();
            foreach(var item in path)
            {
                if(item.isShow == false)
                item.isShow = true;
                else
                item.isShow = false;
            }
           await OrderRepository.UpdateRange(path);
            return new APIResult<int>
            {
                Code = ResultCode.success,
                Mag = "",
                data = 1
            };
        }
    }
}
